/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.annotation.WebServlet;

/**
 *
 * @author Yongke
 */
@WebServlet(name="roomServlet", urlPatterns={"/roomServlet"})
public class roomServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost/myrc?" + "user=progin&password=progin");
            getCategoryTree(0, request, response, conn, 0);
        }
        catch (SQLException ex)
        {
            out.println("SQLException: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex)
        {
            out.println(ex.toString());
        }
        finally {
            out.close();
        }
    }

    private void getCategoryTree(int rootcategory, HttpServletRequest request, HttpServletResponse response, Connection conn, int level)
        throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            stmt = conn.createStatement();
            String qry = "SELECT * FROM subkategori WHERE ID_Kategori=" + rootcategory;
            if (stmt.execute(qry))
            {
                rs = stmt.getResultSet();
            }
            while (rs.next())
            {
                for (int k = 0; k < level; k++)
                {
                    out.print("&nbsp;&nbsp;&nbsp;");
                }
                out.println(rs.getInt("ID_SubKategori") + " " + rs.getString("Nama_SubKategori") + "<br />");
                if (memori.Room.getAllRoomList().isEmpty())
                {
                }
                else
                {
                    for (int i = 0; i < memori.Room.getAllRoomList().size(); i++)
                    {
                        if (request.getSession().getAttribute("userrole").equals("admin"))
                        {
                            if (memori.Room.getAllRoomList().elementAt(i).getCategoryID() == rs.getInt("ID_SubKategori"))
                            {
                                for (int k = 0; k <= level; k++)
                                {
                                    out.print("&nbsp;&nbsp;&nbsp;");
                                }
                                out.println(memori.Room.getAllRoomList().elementAt(i).getRoomName() + "<br />");
                            }
                        }
                        else
                        {
                            if (memori.Room.getAllRoomList().elementAt(i).getCategoryID() == rs.getInt("ID_SubKategori"))
                            {
                                for (int k = 0; k <= level; k++)
                                {
                                    out.print("&nbsp;&nbsp;&nbsp;");
                                }
                                out.println("<a href=\"javascript:newRoomTab('" + memori.Room.getAllRoomList().elementAt(i).getRoomName()
                                        + "'," + memori.Room.getAllRoomList().elementAt(i).getCategoryID()
                                        + ")\">" + memori.Room.getAllRoomList().elementAt(i).getRoomName() + "</a><br />");
                            }
                        }
                    }
                }
                int next = level + 1;
                getCategoryTree(rs.getInt("ID_SubKategori"), request, response, conn, next);
            }
        }
        catch (SQLException ex)
        {
            out.println("SQLException: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex)
        {
            out.println(ex.toString());
        }
        finally {
            if (rs != null) {
                try {
                rs.close();
                } catch (SQLException sqlEx) { } // ignore
                rs = null;
            }
            if (stmt != null) {
                try {
                stmt.close();
                } catch (SQLException sqlEx) { } // ignore
                stmt = null;
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
